package com.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * properties 工具类
 * @author SWP 
 * @date 2016年11月4日 下午2:39:49
 */
public class SystemProUtils {
	private static final SystemProUtils single = new SystemProUtils();  
	private static Properties PROPERTIES;
    private SystemProUtils() {
    	PROPERTIES = new Properties();
    	InputStream in = getClass().getResourceAsStream("/system.properties");
    	try {
			PROPERTIES.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
    	
    }  
    //静态工厂方法   
    public static SystemProUtils getInstance() {  
        return single;  
    }
    public Object get(String key){
    	return PROPERTIES.get(key);
    }
    public String getProperty(String key){
    	if(PROPERTIES.getProperty(key)==null){
    		return "";
    	}else{
    		try {
				return new String(PROPERTIES.getProperty(key).getBytes("iso-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "";
			}
    		//return PROPERTIES.getProperty(key);
    	}
    }
    /** 
    * @Title: getInteger 
    * @Description: TODO(获取int 异常返回-99) 
    * @param key
    * @return  Integer
    */
    public Integer getInteger(String key){
    	try {
			return Integer.parseInt(getProperty(key));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return -99;
		}
    }
    /**
     * 获取键名以StatKeyName开头的键值对Map
     * @param StatKeyName
     * @return Map<String, String>
     */
    public  Map<String, String>  getPropertyByStatKeyName(String StatKeyName){
    	Enumeration en = PROPERTIES.propertyNames();
    	Map<String, String> map = new HashMap<String, String>();
        while(en.hasMoreElements()){
           String key = (String)en.nextElement();
           String value = getProperty(key);
            if(key.startsWith(StatKeyName)){
 	            map.put(key, value);
            }
        }
    	return map;
    }
}
